package com.bigdata.schaduler.domain;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.bigdata.schaduler.util.Param;
import lombok.Data;
import org.apache.dolphinscheduler.dao.entity.Schedule;

@Data
public class ScheduleDefineParam {

  @Param
  private ScheduleParam schedule;

  private String failureStrategy = "END";

  private String warningType = "NONE";

  private String processInstancePriority = "MEDIUM";

  private String warningGroupId = "0";

  private String workerGroup = "default";

  private String environmentCode = "";

  @Param
  private Long processDefinitionCode;

  @Data
  public static class ScheduleParam {
    private String startTime;
    private String endTime;
    private String crontab;
    private String timezoneId = "Asia/Shanghai";

    @Override
    public String toString() {
      return JSONUtil.toJsonStr(this);
    }
  }

  public void converterToScheduleDefineParam(Schedule schedule) {
    BeanUtil.copyProperties(schedule, this);
    ScheduleParam scheduleParam = new ScheduleParam();
    scheduleParam.setStartTime(DateUtil.formatDateTime(schedule.getStartTime()));
    scheduleParam.setEndTime(DateUtil.formatDateTime(schedule.getEndTime()));
    scheduleParam.setTimezoneId(schedule.getTimezoneId());
    scheduleParam.setCrontab(schedule.getCrontab());
    this.setSchedule(scheduleParam);
  }
}